const { classApi } = require('../../../api/services/spTroubleClass/index')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    params: { pageSize: 15, pageNum: 1, name: '' },
    classList: [],
    isLoading: false, //节流状态值
    dataMore: true,
  },
  // 搜索
  async onSearch(event) {
    this.setData({ name: event.detail })
    let params = { name: event.detail }
    const res = await classApi(params)
    this.setData({ classList: res.rows })
  },

  async getClassList() {
    this.setData({ isLoading: true })
    if (!this.data.dataMore) return
    const res = await classApi(this.data.params)

    if (res.code != 200) {
      return app.toast(res.msg)
    }
    const newList = [...this.data.classList, ...res.rows]
    this.setData({
      classList: newList,
      isLoading: false,
      dataMore: newList.length < res.total,
    })
  },

  handleItem(event) {
    let { item } = event.currentTarget.dataset
    console.log(item)
    let pages = getCurrentPages()
    let currPage = pages[pages.length - 1] //当前页面
    let prevPage = pages[pages.length - 2] //上一个页面

    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去

    let troubleClass = item.name
    let classId = item.id

    prevPage.setData({
      troubleState: '',
      checkBasis: '',
      troubleSite: '',
      troubleSuggest: '',
      'item.troubleState': '',
      'item.checkBasis': '',
      'item.troubleSite': '',
      'item.troubleSuggest': '',
    })

    //不需要页面更新
    prevPage.setData({
      troubleClass: troubleClass,
      classId: classId,
      'item.troubleClass': troubleClass,
    })

    wx.navigateBack({
      delta: 1,
    })
  },

  //取消
  onCancel() {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getClassList()
  },

  onReachBottom: function () {
    if (this.data.isLoading) return
    this.data.params.pageNum++
    this.getClassList()
  },
})
